package com.cxxx.guns.modular.backend.service.impl;

import com.cxxx.guns.core.base.tips.SuccessTip;
import com.cxxx.guns.core.common.exception.BizExceptionEnum;
import com.cxxx.guns.core.exception.GunsException;
import com.cxxx.guns.core.shiro.ShiroKit;
import com.cxxx.guns.core.util.ToolUtil;
import com.cxxx.guns.modular.backend.common.Const;
import com.cxxx.guns.modular.backend.model.Announcement;
import com.cxxx.guns.modular.backend.dao.AnnouncementMapper;
import com.cxxx.guns.modular.backend.service.IAnnouncementService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cxxx.guns.modular.system.model.User;
import com.cxxx.guns.modular.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author cczhang
 * @since 2018-09-05
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {
    @Autowired
    private IUserService userService;

    private static SuccessTip SUCCESS_TIP = new SuccessTip();
    @Override
    public Object addAnnouncement(Announcement announcement) {
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = this.userService.selectById(userId);
        announcement.setUserId(userId);
        announcement.setAuthor("匿名");
        if(ToolUtil.isNotEmpty(user.getName())){
            announcement.setAuthor(user.getName());
        }
        if(ToolUtil.equals(user.getRoleid(),1)){
            announcement.setStatus(Const.JiaXiaoStatusEnum.SHEN_HE_TONG_GUO.getCode());
        }else {
            announcement.setStatus(Const.JiaXiaoStatusEnum.WEI_SHEN_HE.getCode());
        }
        this.baseMapper.insert(announcement);
        return SUCCESS_TIP;
    }

    @Override
    public List<Announcement> getListIndex(int status) {
        return this.baseMapper.getListIndex(status);
    }

    @Override
    public Announcement selectNext(Date date) {
        return this.baseMapper.selectNext(date);
    }

    @Override
    public Announcement selectPrevious(Date date) {
        return this.baseMapper.selectPrevious(date);
    }
}
